import { MessageType } from "../type/messageType.ts";
import friendsSocketApiFunc from "./socketFunc/friends.ts";
import { AddFriends, MessageListItem } from "../type/socketHandelFuncType.ts";
import type { BaseMessage, FriendMessage } from "@/api/xhr/message";
function registerEvent(socket) {
  // 注册事件
  socket.on("connect_error", err => {
    // 链接出错的地方，权限不对 退出登录 或者 重新链接 一直链接不上再退出登录
    console.log(err.message); // not authorized
    console.log(err.data); // { content: "Please retry later" }
  });

  socket.on(
    MessageType.ADDFRIEND,
    (args: AddFriends) =>
      friendsSocketApiFunc.addFriend && friendsSocketApiFunc.addFriend(args)
  );

  socket.on(
    MessageType.CONFIRMFRIEND,
    (args: MessageListItem) =>
      friendsSocketApiFunc.comfirmFriend &&
      friendsSocketApiFunc.comfirmFriend(args)
  );

  socket.on(
    MessageType.CREATEGROUP,
    (args: MessageListItem) =>
      friendsSocketApiFunc.createdGroup &&
      friendsSocketApiFunc.createdGroup(args)
  );

  socket.on(
    MessageType.DESTROYGROUP,
    (args: any) =>
      friendsSocketApiFunc.destroyGroup &&
      friendsSocketApiFunc.destroyGroup(args)
  );

  socket.on(
    MessageType.FRIENDMESSAGE,
    (args: BaseMessage<FriendMessage>) =>
      friendsSocketApiFunc.getFriendMessage &&
      friendsSocketApiFunc.getFriendMessage(args)
  );
  socket.on(
    MessageType.MESSAGELIST,
    (args: any) =>
      friendsSocketApiFunc.getMessagelist &&
      friendsSocketApiFunc.getMessagelist(args)
  );
  socket.on(
    MessageType.GROUPMESSAGE,
    (args: any) =>
      friendsSocketApiFunc.getgroupMessage &&
      friendsSocketApiFunc.getgroupMessage(args)
  );
  
  socket.on(
    MessageType.RECALL_MESSAGE,
    (args: { messageId: string | number, type: string }) =>
      friendsSocketApiFunc.recallMessage &&
      friendsSocketApiFunc.recallMessage(args)
  );

  // 添加新的事件监听用户被邀请进群
  socket.on(
    MessageType.INVITED_TO_GROUP,
    (args: MessageListItem) => 
      friendsSocketApiFunc.invitedToGroup && 
      friendsSocketApiFunc.invitedToGroup(args)
  );
}
export default registerEvent;
